<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true">
            <el-form-item label="结算单编码">
                <el-input v-model="queryParams.code" placeholder="请输入结算单编码" clearable style="width: 200px" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="付款单位">
                <el-input v-model="queryParams.collectionDeptName" placeholder="请输入付款单位" clearable style="width: 200px"
                    @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="收款方式">
                <el-select v-model="queryParams.collectionType" clearable class="m-2" placeholder="选择收款方式"
                    style="width: 200px;">
                    <el-option v-for="item in mes_collection_type" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select>

            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Download" @click="handleExport">导出</el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" border :data="finalStatementList" style="margin-top: 20px;">
            <el-table-column label="序号" width="60" type="index" align="center">
            </el-table-column>
            <el-table-column label="状态" width="120" align="center" prop="status">
                <template #default="scope">
                    <dict-tag :options="mes_handle_state" :value="scope.row.status" />
                </template>
            </el-table-column>
            <el-table-column label="结算单编码" align="center" prop="code" width="200">
                <template v-slot="{ row }">
                    <el-button type="primary" @click="handleView(row)" text>{{ row.code
                    }}</el-button>
                </template>
            </el-table-column>
            <el-table-column label="收款日期" width="120" align="center" prop="collectionDate" />
            <el-table-column label="收款项目" align="center" prop="collectionProject" />

            <el-table-column label="付款单位" align="center" prop="collectionDeptName" />
            <el-table-column label="收款方式" align="center" prop="collectionType">
                <template #default="scope">
                    <dict-tag :options="mes_collection_type" :value="scope.row.collectionType" />
                </template>
            </el-table-column>

            <el-table-column label="收款事由" align="center" prop="collectionReason" />

            <el-table-column label="操作" fixed="right" align="center" min-width="150" class-name="small-padding fixed-width">
                <template #default=scope>
                    <el-tooltip content="编辑" placement="top" v-if="scope.row.status ==0">
                        <el-button link type="primary" icon="Edit" @click="handleEdit(scope.row)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top" v-if="scope.row.status ==0">
                        <el-button link type="primary" icon="Delete" @click="handleDle(scope.row)"></el-button>
                    </el-tooltip>
                    <!-- <el-button link type="primary" icon="Printer" v-if="scope.row.status ==0" @click="handleService(scope.row)">提交</el-button> -->
                </template>
            </el-table-column>
        </el-table>
        <pagination style="margin-top: 0;padding-top: 0;" v-show="total > 0" :total="total"
            v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </div>
</template>
 
<script setup name="handle">
import { listWastesettle, updateWastesettle, delWastesettle,wasteExport } from '@/api/scrap/finalStatement.js'

const { proxy } = getCurrentInstance();

const { mes_handle_state, mes_collection_type } = proxy.useDict('mes_handle_state', 'mes_collection_type');
import { downLoadBlob } from '@/utils/ruoyi'
import { ElMessage, ElMessageBox } from 'element-plus'
const router = useRouter()
const finalStatementList = ref([]);
const loading = ref(false);
const total = ref(0);
const showSearch = ref(true);

const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        collectionDeptName: null,
        collectionType: null,

    },
    rules: {
        code: [
            { required: true, message: "处理单编码不能为空", trigger: "blur" }
        ],
        handleDept: [
            { required: true, message: "处理单位不能为空", trigger: "blur" }
        ],
    }
});
const { queryParams, form, rules } = toRefs(data);
// 查看
const handleView = (row) => {
    router.push({
        path: '/scrap/finalStatement/details',
        query: {
            id: row.id,
            readonly: true,
            code: row.code,
            status: row.status
        }
    })
}
// 新增
const handleAdd = () => {
    router.push('/scrap/finalStatement/details')
}
const handleEdit = (row) => {
    router.push({
        path: '/scrap/finalStatement/details',
        query: {
            id: row.id,
            code: row.code,
            status: row.status
        }
    })
}
//提交
const handleService = (row) => {
    ElMessageBox.confirm(`确定要提交结算单编码为 ${row.code} 的处理单吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        // 用户点击了确定按钮
        const formDate = { ...row }
        formDate.status = 1
        updateWastesettle(formDate).then(response => {
            if (response.code === 200) {
                ElMessage({
                    message: '提交成功',
                    type: 'success',
                });
                getList();
            }
        }).catch(() => {
            row.status = 0
        });
    })
};
//删除
const handleDle = (row) => {
    ElMessageBox.confirm('确定要删除编码为 ' + row.code + ' 的处理单吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
    }).then(() => {
        // 用户点击了确定按钮
        delWastesettle(row.id).then(response => {
            if (response.code === 200) {
                ElMessage({
                    message: '删除成功',
                    type: 'success',
                });
                getList();
            }
        });
    }).catch(() => {
        //取消
    });
};

function getList() {
    loading.value = true;
    listWastesettle(queryParams.value).then(response => {
        finalStatementList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    queryParams.value.code = null
    queryParams.value.collectionType = null
    queryParams.value.collectionDeptName = null
    proxy.resetForm("queryRef");
    handleQuery();
}
// 导出
const handleExport = () => {
    wasteExport(queryParams.value).then(res => {
        downLoadBlob(res, '废料处理列表')
    })
}
getList();
</script>
 